package com.mp.approvalreq.util;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ValidateUtil {
    private static final int[] WEIGHT = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9,
            10, 5, 8, 4, 2};
    private static final char CHECK_CODE[] = {'1', '0', 'X', '9', '8', '7',
            '6', '5', '4', '3', '2'};

    private static final Pattern TELEPHONE = Pattern
            .compile("0\\d{2,3}-\\d{7,8}");


    private static final Pattern MOBILENO = Pattern
            .compile("^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\\d{8}$");


    private static final Pattern EMAIL = Pattern
            .compile("[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?");

    private static final Pattern VALID_MONTH = Pattern
            .compile("^2([0-9][0-9][0-9])-(([1-9])|([0][1-9])|([1][0-2]))");

    /**
     * 身份证号验证
     *
     * @param id
     * @return
     * @throws Throwable
     */
    public static boolean isValidId(String id) throws Throwable {
        if (StringUtils.isEmpty(id) || id.length() != 18) {
            return false;
        }

        int sum = 0;
        int num;
        for (int index = 0; index < 17; index++) {
            num = id.charAt(index) - '0';
            if (num < 0 || num > 9) {
                return false;
            }
            sum += num * WEIGHT[index];
        }
        return CHECK_CODE[sum % 11] == Character.toUpperCase(id.charAt(17));
    }

    /**
     * 固定电话号码验证
     *
     * @param phonenumber
     * @return
     */
    public static boolean isTelephone(String phonenumber) {
        if (StringUtils.isEmpty(phonenumber)) {
            return false;
        }

        Matcher m = TELEPHONE.matcher(phonenumber);
        return m.matches();
    }

    /**
     * 手机号码验证
     *
     * @param mobiles
     * @return
     */
    public static boolean isMobileNO(String mobiles) {
        // Pattern p =
        // Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
        if (StringUtils.isEmpty(mobiles)) {
            return false;
        }
/*		Pattern p = Pattern
				.compile("^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\\d{8}$");*/
        Matcher m = MOBILENO.matcher(mobiles);
        return m.matches();
    }

    /**
     * 邮箱验证
     *
     * @param email
     * @return
     */
    //
    public static boolean isEmail(String email) {
        if (StringUtils.isEmpty(email)) {
            return false;
        }

        Matcher m = EMAIL.matcher(email);
        return m.matches();
    }

    /**
     * 判断日期格式是否正确 yyyy-mm-dd
     *
     * @param sDate
     * @return
     */
    public static boolean isValidDate(String sDate) {
        if (StringUtils.isEmpty(sDate)) {
            return false;
        }
        String datePattern1 = "\\d{4}-\\d{2}-\\d{2}";
        String datePattern2 = "^((\\d{2}(([02468][048])|([13579][26]))"
                + "[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|"
                + "(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?"
                + "((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?("
                + "(((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?"
                + "((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
        if ((sDate != null)) {
            Pattern pattern = Pattern.compile(datePattern1);
            Matcher match = pattern.matcher(sDate);
            if (match.matches()) {
                pattern = Pattern.compile(datePattern2);
                match = pattern.matcher(sDate);
                return match.matches();
            } else {
                return false;
            }
        }
        return false;
    }


    /**
     * 判断日期格式: yyyy-mm
     *
     * @param month
     * @return
     */
    public static boolean isValidMonth(String month) {
        if (StringUtils.isEmpty(month)) {
            return false;
        }

        Matcher match = VALID_MONTH.matcher(month);
        return match.matches();
    }


    /**
     * 判断日期格式是否正确 yyyy-mm-dd HH:MM
     *
     * @param sDateTime
     * @return
     */
    public static boolean isValidDateTime(String sDateTime) {
        if (StringUtils.isEmpty(sDateTime)) {
            return false;
        }
        Pattern pattern = Pattern
                .compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1][0-9])|([2][0-3]))\\:([0-5]?[0-9])))?$");
        Matcher match = pattern.matcher(sDateTime);
        return match.matches();
    }

    /**
     * 判断日期格式是否正确 yyyy-mm-dd HH:MM:ss
     *
     * @param sDateTime
     * @return
     */
    public static boolean isValidDateTimeMills(String sDateTime) {
        if (StringUtils.isEmpty(sDateTime)) {
            return false;
        }
        Pattern pattern = Pattern
                .compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1][0-9])|([2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
        Matcher match = pattern.matcher(sDateTime);
        return match.matches();
    }

    /**
     * 校验金额的正则表达式: 只能以0-9开头,必须且只能带两个小数点
     * 0开头: 0.00(正确); 0(错误); 0.0(错误); 0.020(错误); 0.231(错误); 0.10(正确)
     * 1-9开头: 1.23(正确); 123.10(正确); 123.1(错误); 78(错误); 23.00(正确); 43.0(错误);
     */
//	public static boolean isMoney(String input) {
//		Pattern pattern = Pattern.compile("(^[1-9]([0-9]+)(\\.[0-9][0-9]))|(^[0-9]\\.[0-9][0-9])");
//		Matcher matcher = pattern.matcher(input);
//		return matcher.matches();
//	}
    //
    public static boolean isMoney(String input) {
        if (StringUtils.isEmpty(input)) {
            return false;
        }
        Pattern pattern = Pattern.compile("(^(?:[1-9]\\d*|0)(?:\\.\\d+)?$)");
        Matcher matcher = pattern.matcher(input);
        return matcher.matches();
    }

    /**
     * 金额格式验证（最多两位小数）
     *
     * @param str
     * @return
     */
    public static boolean isNumber(String str) {
        if (StringUtils.isEmpty(str)) {
            return false;
        }
        Pattern pattern = Pattern
                .compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$");
        Matcher match = pattern.matcher(str);
        return match.matches();
    }

    public static boolean isNotInteger(String str) {
        return !isNumber(str);
    }

    /**
     * 是否为正整数
     *
     * @return
     */
    public static boolean isPositiveInteger(String str) {
        if (!isInteger(str)) {
            return false;
        }
        return Integer.parseInt(str) > 0;
    }

    /**
     * 判断是否是整数
     *
     * @param str
     * @return
     */
    public static boolean isInteger(String str) {
        if (StringUtils.isEmpty(str)) {
            return false;
        }
        Pattern pattern = Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))?$");
        Matcher match = pattern.matcher(str);
        return match.matches();
    }

    public static boolean isDouble(String str) {
        try {
            Double.parseDouble(str);
        } catch (Throwable e) {
            return false;
        }
        return true;
    }

    public static void main(String[] args) {

        System.out.println("44:" + isMoney("44"));
    }


}
